package com.Adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.myandroidactualcombat.R;
import com.example.myandroidactualcombat.data.Collection;

import java.util.List;

public class CollectionAdapter extends ArrayAdapter<Collection> {
    private int resourceId;
    public CollectionAdapter(@NonNull Context context, int textViewResourceId, @NonNull List<Collection> objects) {
        super(context, textViewResourceId, objects);
        resourceId=textViewResourceId;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        //因为这个getview需要运行多次，所以getItem(position)就是运行到了第几个，有几条listview条目就运行几次
        Collection collection=getItem(position);
        View view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
        ImageView iv_bookjpg=view.findViewById(R.id.iv_bookjpg);
        TextView tv_written=view.findViewById(R.id.tv_written);
        ImageView iv_love=view.findViewById(R.id.iv_love);
        if (collection.isIfcollection()){
            iv_love.setImageResource(R.drawable.love);
        }else{
            iv_love.setImageResource(R.drawable.no_love);
        }
        iv_bookjpg.setImageResource(collection.getImageId());
        tv_written.setText(collection.getBookname());
        return  view;
    }
}
